using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NCS.Module.CMS.Library;
using NCS.Module.CMS.Library.Definitions;
using NCS.Module.CMS.Library.Distributions;
using NCS.Library.Utilities;
using NCS.Module.CMS.Library.Contents;

namespace NCS.Module.CMS.Web.Movies
{
    public partial class SearchResult : CMSWebControl
    {
        public string lblPage = "";
        public int Total = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            int totalRecord;
            if (Request.QueryString["page"] != null)
            {
                txtHiddenValue.Text = ConvertUtility.ToString((ConvertUtility.ToInt32(Request.QueryString["page"]) - 1));
            }
            else
            {
                txtHiddenValue.Text = "0";
            }
            string keysearch = Request.QueryString["keysearch"];
            int pagesize = 10;
            DataTable dtContents = DistributionController.SearchForWeb(pagesize, ConvertUtility.ToInt32(txtHiddenValue.Text), out totalRecord, keysearch);
            rptContentList.DataSource = dtContents;
            rptContentList.ItemDataBound += new RepeaterItemEventHandler(rptContentList_ItemDataBound);
            rptContentList.DataBind();
            Total = totalRecord;
            #region paging

            double pagenum = totalRecord % pagesize;
            if (pagenum == 0)
            {
                for (int i = 1; i < totalRecord / pagesize + 1; i++)
                {
                    if (ConvertUtility.ToInt32(txtHiddenValue.Text) == 0)
                    {
                        if (i == 1)
                        {
                            lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString()) + "'> <b>Trang " + i + "</b></a>" + "&nbsp;|&nbsp;";
                        }
                        else
                        {
                            lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString()) + "'>" + i + "</a>" + "&nbsp;|&nbsp;";
                        }
                    }
                    else if (i == ConvertUtility.ToInt32(txtHiddenValue.Text) + 1)
                    {
                        lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString()) + "'> <b>Trang " + i + "</b></a>" + "&nbsp;|&nbsp;";
                    }
                    else
                    {
                        lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString()) + "'>" + i + "</a>" + "&nbsp;|&nbsp;";
                    }
                }
            }
            else if (totalRecord % pagesize > 0)
            {
                for (int i = 1; i < totalRecord / pagesize + 2; i++)
                {
                    if (ConvertUtility.ToInt32(txtHiddenValue.Text) == 0)
                    {
                        if (i == 1)
                        {
                            lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString()) + "'> <b>Trang " + i + "</b></a>" + "&nbsp;|&nbsp;";
                        }
                        else
                        {
                            lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString()) + "'>" + i + "</a>" + "&nbsp;|&nbsp;";
                        }
                    }
                    else if (i == ConvertUtility.ToInt32(txtHiddenValue.Text) + 1)
                    {
                        lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString()) + "'> <b>Trang " + i + "</b></a>" + "&nbsp;|&nbsp;";
                    }
                    else
                    {
                        lblPage += "<a href='" + MiscUtility.UpdateQueryStringItem(HttpContext.Current.Request, "page", i.ToString()) + "'>" + i + "</a>" + "&nbsp;|&nbsp;";
                    }
                }
            }
            #endregion
        }

        void rptContentList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView currow = (DataRowView)e.Item.DataItem;
                Label lbalias = (Label)e.Item.FindControl("lbalias");
                lbalias.Text = (ConvertUtility.ToString(currow["Content_Alias"]) == string.Empty ? "" : (currow["Content_Alias"].ToString() + "  ")) + ConvertUtility.FormatTimeVn(ConvertUtility.ToDateTime(currow["Distribution_CreateDate"]));
                HyperLink hplcontent = (HyperLink)e.Item.FindControl("hplcontent");
                hplcontent.Text = currow["Content_Headline"].ToString();
                hplcontent.NavigateUrl = BuildUrlByDistId((int)currow["Distribution_ID"], (int)currow["Distribution_ZoneID"]);
            }
        }    
    }
}